package com.ruoyi.management.caronlinemonitor.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.ruoyi.constant.TaxiConstant;
import com.ruoyi.management.caronlinemonitor.mapper.CarOnLineMonitorMapper;
import com.ruoyi.management.caronlinemonitor.ro.GetCarOnLineMonitorParam;
import com.ruoyi.management.caronlinemonitor.service.CarOnLineMonitorService;
import com.ruoyi.management.caronlinemonitor.vo.GetCarOnLineMonitorVo;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * @description: 车辆在线监控
 * @author: xly
 * @date: 2021-12-27
 */
@Service
public class CarOnLineMonitorServiceImpl implements CarOnLineMonitorService {

    @Autowired
    private CarOnLineMonitorMapper carOnLineMonitorMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<GetCarOnLineMonitorVo> list(GetCarOnLineMonitorParam param) {
        List<GetCarOnLineMonitorVo> list = carOnLineMonitorMapper.list(param);
        for (GetCarOnLineMonitorVo vo : list) {
            //空车数量
            String emptyKey = CharSequenceUtil.format(TaxiConstant.EMPTY_NUM_CAR_KEY, DateUtil.format(DateUtil.date(), "yyyyMMdd"),vo.getCompanyId());
            Integer emptyNum = (Integer) redisTemplate.opsForValue().get(emptyKey);
            if(null == emptyNum){
                emptyNum = 0;
            }
            vo.setEmptyNum(emptyNum);
            //重车数量
            String heavyKey = CharSequenceUtil.format(TaxiConstant.HEAVY_NUM_CAR_KEY, DateUtil.format(DateUtil.date(), "yyyyMMdd"),vo.getCompanyId());
            Integer heavyNum = (Integer) redisTemplate.opsForValue().get(heavyKey);
            if(null == heavyNum){
                heavyNum = 0;
            }
            vo.setHeavyNum(heavyNum);
            //在线车辆
            String redisKey = CharSequenceUtil.format(TaxiConstant.ONLINE_CAR_REDIS_KEY, DateUtil.format(DateUtil.date(), "yyyyMMdd"),vo.getCompanyId());
            Integer onLineNum = (Integer) redisTemplate.opsForValue().get(redisKey);
            if(null == onLineNum){
                onLineNum = 0;
            }
            //占有率
            BigDecimal onLineDecimal = new BigDecimal(onLineNum.toString());
            Integer operateNum = vo.getOperateNum();
            if(operateNum == null){
                operateNum = 0;
            }
            BigDecimal operateDecimal = new BigDecimal(operateNum.toString());
            BigDecimal share = onLineDecimal.multiply(new BigDecimal(100)).divide(operateDecimal, 2, BigDecimal.ROUND_HALF_UP);
            vo.setOnLineNumAndShare(onLineNum + "/" + share + "%");
            vo.setOffLineNum(operateNum - emptyNum - heavyNum);
        }
        return list;
    }
}
